package com.matt.repository.materials;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.matt.domain.materials.MaterielCategory;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MaterielCategoryRepository extends JpaRepository<MaterielCategory, String>, JpaSpecificationExecutor<MaterielCategory> {

    List<MaterielCategory> findAllByAreaIdOrAreaIdIsNullOrderBySortDesc(String areaId);


    @Query(value = "select * from materiel_category mc where mc.name = ?2 and (mc.areaId is null or mc.areaId = ?1)", nativeQuery = true)
    MaterielCategory findFirstByAreaIdBothDefaultAndName(String areaId, String name);

    @Query(value = "select * from materiel_category mc where mc.id = ?2 and (mc.areaId is null or mc.areaId = ?1)", nativeQuery = true)
    MaterielCategory findFirstByAreaIdBothDefaultAndId(String areaId, String cid);


}